angular.module("module.goodsMoneyStatistic",[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('home.goodsMoneyStatistic',{
                url:"/goodsMoneyStatistic",
                templateUrl:"adminInfo/goodsMoneyStatistic.html",
                controller:'GoodsMoneyStatisticController',
                controllerAs:'vm'
            })
    }])
    .controller('GoodsMoneyStatisticController',[function () {
        var vm=this;
        function check() {
            winAlert({title:'请验证财务统计密码',body:template('checkPassword'),cancel:'取消',fun:function(){
                var password=$(".panel input").val();
                console.log("password",password);
                if (password == moneyStatisticsPassword){
                    checked=true;
                    $(".panel .close").trigger("click");
                    loadpage()
                }else {
                    return toast('密码不正确，请重新输入')
                }
            }});
        }

        function loadpage(){
            if(!checked){//该判断是为了防止点击分店列表中分店选项直接刷新页面而跳过验证密码的安全问题
                check();
            }else {
                console.log("shopId", shopId);
                data.shopId = shopId;
                console.log("data", data);
                $('.container-fluid').html(template('list_tmpl', data));
                $('.datetime').daterangepicker({
                    format: 'YYYY-MM-DD HH:mm', timePicker: true,
                    timePickerSeconds: true, timePickerIncrement: 1, timePicker12Hour: false
                }, function (start, end) {
                    data.startTime = start.format('YYYY-MM-DD HH:mm');
                    data.endTime = end.format('YYYY-MM-DD HH:mm');
                    $(':input[name=startTime]').val(data.startTime);
                    $(':input[name=endTime]').val(data.endTime);
                });
                $('.daterang').on('click', function () {
                    var day = parseInt($(this).attr('data-day'), 10);
                    data.startTime = moment().subtract(day, 'days').format('YYYY-MM-DD 00:00');
                    data.endTime = moment().format('YYYY-MM-DD HH:mm');
                    $(':input[name=startTime]').val(data.startTime);
                    $(':input[name=endTime]').val(data.endTime);
                    return false;
                });
                $('.print').on('click', function () {
                    var content = ''; //
                    content += '筛选时间：' + data.startTime + '至' + data.endTime + "#";
                    content += '筛选出：共计 ' + data.totalRoom + ' 个包厢，' + data.totalOrder + ' 笔订单，#';
                    content += '累计消费：' + data.totalMoney + '（其中微信支付：' + data.totalWxMoney + ' 会员支付：' + data.totalHyMoney + '）#';
                    content += '此时间段充值：' + data.cz + '，积分兑换：' + data.jf + ' ，退款金额：' + data.refundMoney;
                    $.post('goodsMoneyTJPrint', {content: content}, function (res) {
                        try {
                            res = JSON.parse(res);
                        } catch (e) {
                            toast(e);
                        }
                        if (!res.succeed) return toast('打印失败，失败原因：' + res.message);
                        toast('打印成功...', 'success');
                    });
                });
                $('.xqing').on('click', function () {
                    var i = $(this).attr('data-i');
                    var vo = data.tj[i];
                    var para = {startTime: data.startTime, endTime: data.endTime, shopId: shopId, roomId: vo.roomId};
                    $.getJSON('goodsMoneyOrderTJ', para, function (res) {
                        if (!res.succeed) return toast('操作失败，失败原因：' + res.message);
                        winAlert({title: '包厢' + vo.roomId, body: template('order_tmpl', res.data)});
                        $('.chakan').on('click', function () {
                            var i = $(this).attr('data-i');
                            var li = res.data.tj[i];
                            $.getJSON('orderGoodsList?orderNo=' + li.orderNo, function (rs) {
                                if (!rs.succeed) return toast('获取数据失败，失败原因：' + rs.message);
                                rs.data = rs.data || {};
                                rs.data.isScore = li.jf > 0;
                                winAlert({body: template('order_goods_tmpl', rs.data), title: '订单商品列表-包厢' + vo.roomId});
                            });
                        });
                    });
                });
                $('.search').on('click', function () {
                    search()
                });

                function search() {
                    var para = {};
                    paraList = $("input.form-control");
                    for (var i = 0; i < paraList.length; i++) {
//        console.log("paraList.eq(i)=",paraList.eq(i));
//        console.log("paraList.eq(i).attr('name')=",paraList.eq(i).attr("name"));
                        var str = paraList.eq(i).attr("name") + '';
//        console.log("paraList.eq(i).val()=",paraList.eq(i).val());
                        para[str] = paraList.eq(i).val();
                    }
                    console.log("para", para);
                    $.getJSON("goodsMoneyTJList", para, function (res) {
                        console.log("goodsMoneyTJList_res", res);
                        data = res.data;
                        console.log("goodsMoneyTJList_data", data);
                        data.unknowMoney=
                            ( parseFloat(data.totalMoney)
                                - parseFloat(data.totalWxMoney)
                                - parseFloat(data.totalHyMoney)
                                - parseFloat(data.totalSkmMoney)
                                - parseFloat(data.totalPosMoney)
                                - parseFloat(data.totalCashMoney)
                                - parseFloat(data.totalAlpayMoney)).toFixed(2)+"元";
                        loadpage();
                    })
                }
            }
        }
        check();
    }]);